package com.lt.flashcard.view;

import java.io.File;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Div;
import org.zkoss.zul.Include;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.Repository;

public class AdminImportDataCtrl extends SelectorComposer<Component> {

	private static final long serialVersionUID = 1L;

	private final String FLASHCARDS_FILES_PATH = Utility.getWebAppPath() + Utility.getConfiguration("com.lt.flashcard.flashcardFilesToImportFolder","files_to_import\\flashcards\\");
	private final String FLASHCARDSETS_FILES_PATH = Utility.getWebAppPath() + Utility.getConfiguration("com.lt.flashcard.flashcardSetFilesToImportFolder","files_to_import\\flashcardsets\\");

	@Wire
	private Combobox cbxDataType; 
	@Wire
	private Div import_data;
	
	private User user;
	private List<File> files = null;
	
	/**
	 * @return the files
	 */
	public List<File> getFiles() {
		return files;
	}

	/**
	 * @param files the files to set
	 */
	public void setFiles(List<File> files) {
		this.files = files;
	}

	
	/* (non-Javadoc)
	 * @see org.zkoss.zk.ui.select.SelectorComposer#doBeforeComposeChildren(org.zkoss.zk.ui.Component)
	 */
	@Override
	public void doBeforeComposeChildren(Component comp) throws Exception {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		AAService srv = Repository.getInstance().getAAService();
		if (!srv.isLogin() || !srv.hasRole("admin")) {
			Executions.sendRedirect("/admin_login.zul");
		}
		user = srv.getCurrentUser();
		super.doBeforeComposeChildren(comp);
	}
	
	public User getUser(){
		return user;
	}
	
	public void setUser(User user){
		this.user = user;
	}
	
	/**
	 * @return the cbxDataType
	 */
	public Combobox getCbxDataType() {
		return cbxDataType;
	}

	/**
	 * @param cbxDataType the cbxDataType to set
	 */
	public void setCbxDataType(Combobox cbxDataType) {
		this.cbxDataType = cbxDataType;
	}

	/**
	 * @return the import_data
	 */
	public Div getImport_data() {
		return import_data;
	}

	/**
	 * @param import_data the import_data to set
	 */
	public void setImport_data(Div import_data) {
		this.import_data = import_data;
	}

	
	@Listen("onSelect=#cbxDataType")
	public void onSelect(Event event) {
		Comboitem selectedItem = (cbxDataType.getSelectedItem());
		String selectedValue = selectedItem.getValue().toString();
		if(!selectedValue.isEmpty()) {
			Include incImportData = new Include("v_import_data.zul");
			incImportData.setDynamicProperty("import_mode", selectedValue);
			import_data.getChildren().clear();
			import_data.getChildren().add(incImportData);
		}
	}

}
